package com.mmy.controller.train;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.sys.TSysUser;
import com.mmy.domain.train.TTrainBase;
import com.mmy.domain.train.TTrainPlan;
import com.mmy.domain.train.VTrainPlan;
import com.mmy.domain.train.VTrainStation;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.train.impl.TrainBaseServiceImpl;
import com.mmy.service.train.impl.TrainPlanServiceImpl;
import com.mmy.service.train.impl.TrainStationServiceImpl;

/**
 * 类的介绍：列车时刻
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */

@Controller
public class TrainPlanController {

	@Autowired
	TrainPlanServiceImpl trainPlanServiceImpl;
	@Autowired
	TrainBaseServiceImpl trainBaseServiceImpl;
	@Autowired
	TrainStationServiceImpl trainStationServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期修改时间2021.03.17
	 */
	@RequestMapping(value = "/backtotrainplanindex")
	public String backToTrainPlanIndex(HttpServletRequest request, HttpSession session, Model model) {
		// 创建的航班信息有所属旅行社 如果为超级管理员显示全部 为其他有权限的只能查看本旅行社的航班
		// 下拉框测试
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		String strTrainID = request.getParameter("iTrainID");
		List<VTrainPlan> lstTrainPlan = trainPlanServiceImpl.findTrainPlan(sessionSysUser, strTrainID);
		model.addAttribute("lstTrainPlan", lstTrainPlan);
		model.addAttribute("strTrainID", strTrainID);
		return "webbackground/train/TrainPlanIndex";
	}

	/**
	 * 函数功能说明： 异步--列车时刻信息列表数据
	 * 
	 * 
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 *
	 */
	@RequestMapping("/backtrainplanjson")
	@ResponseBody
	public String backTrainPlanJson(HttpServletRequest request, HttpSession session, Model model) {

		// 接收的分页信息处理
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		int startIndex = (page - 1) * rows;
		int count = rows;
		// 搜索条件
		String strTrainOutDate = request.getParameter("trainOutDate");
		String strTrainOutZone = request.getParameter("trainOutZone");
		String strTrainInZone = request.getParameter("trainInZone");
		String strTrainID = request.getParameter("iTrainID");

		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");

		// 表格数据
		List<VTrainPlan> lstTrainPlan = trainPlanServiceImpl.findByOrgID(strTrainID, sessionSysUser, strTrainOutDate,
				strTrainOutZone, strTrainInZone, startIndex, count);

		model.addAttribute("lstTrainPlan", lstTrainPlan);
		// 数据总数量
		int total = trainPlanServiceImpl.queryPlanCount(strTrainID, sessionSysUser, strTrainOutDate, strTrainOutZone,
				strTrainInZone);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", lstTrainPlan);
		ret.put("total", total);
		return JSONObject.toJSONString(ret);
	}

	@RequestMapping(value = "/createtrainplan")
	public String ToCreateTrainPlan(Model model) {

		List<TTrainBase> lstTrainBase = trainBaseServiceImpl.findAll();
		model.addAttribute("trainbaselist", lstTrainBase);
		List<TConstZone> lstConstZone = constZoneServiceImpl.findAllCountriess();
		model.addAttribute("llist", lstConstZone);
		List<VTrainStation> lstTrainStation = trainStationServiceImpl.findAll();
		model.addAttribute("tlist", lstTrainStation);

		return "webbackground/train/createtrainplan";
	}

	@RequestMapping(value = "/trainplaninfo")
	public String ToTrainPlanInfo(HttpServletRequest request, Model model) {

		int iPlanID = Integer.parseInt(request.getParameter("iPlanID"));

		VTrainPlan vTrainPlan = trainPlanServiceImpl.findByID(iPlanID);
		model.addAttribute("vTrainPlan", vTrainPlan);

		return "webbackground/train/trainplaninfo";
	}

	/**
	 * 函数功能说明： 新建列车时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/subcreatetrainplan")
	public String CreateTrainPlan(TTrainPlan tTrainPlan, String startTime, String endTime, HttpSession session)
			throws Exception {

		tTrainPlan.setStartDate(common.formatDate(startTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
		tTrainPlan.setEndDate(common.formatDate(endTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));

		// 获取时长
		tTrainPlan.setLong(common.getHourBetweenTwoDay(startTime, endTime));

		// 获取最大值
		tTrainPlan.setPlanID(common.createPlanCode(UtilConstants.TableName.T_TRAIN_PLAN));

		try {

			tTrainPlan.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tTrainPlan.setCreator(tSysUser.getUserName());
			trainPlanServiceImpl.save(tTrainPlan.getPlanID());
			trainPlanServiceImpl.save(tTrainPlan);
			return "redirect:trainplan";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除列车时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@PostMapping("/deltrainplan")
	@ResponseBody
	public String DelTrainPlan(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstPlanID = new ArrayList<>();

		for (String str : checklist) {
			lstPlanID.add(Integer.parseInt(str));
		}

		trainPlanServiceImpl.delTrainPlan(lstPlanID);

		return "webbackground/train/trainplan";
	}

	/**
	 * 函数功能说明： 修改航班时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdatetrainplan")
	public String EnterUpdateTrainPlan(HttpServletRequest request, Model model) {

		int iPlanID = Integer.parseInt(request.getParameter("iPlanID"));

		TTrainPlan tTrainPlan = trainPlanServiceImpl.findByPlanID(iPlanID);
		model.addAttribute("tTrainPlan", tTrainPlan);
		List<TTrainBase> lstTrainBase = trainBaseServiceImpl.findAll();
		model.addAttribute("trainbaselist", lstTrainBase);
		List<TConstZone> lstConstZone = constZoneServiceImpl.findAllCountriess();
		model.addAttribute("llist", lstConstZone);
		List<VTrainStation> lstTrainStation = trainStationServiceImpl.findAll();
		model.addAttribute("tlist", lstTrainStation);

		return "webbackground/train/updatetrainplan";

	}

	@RequestMapping(value = "/updatetrainplan")
	public String UpdateTrainPlan(TTrainPlan tTrainPlan, HttpSession session) throws Exception {
		try {
//			 定义一个新的实体类对象 
			TTrainPlan dtTrainPlan = trainPlanServiceImpl.findByPlanID(tTrainPlan.getPlanID());
			dtTrainPlan.setStandard(tTrainPlan.getStandard());
			dtTrainPlan.setTrainID(tTrainPlan.getTrainID());
			dtTrainPlan.setZoneOutID(tTrainPlan.getZoneOutID());
			dtTrainPlan.setStationOutID(tTrainPlan.getStationOutID());
			dtTrainPlan.setZoneInID(tTrainPlan.getZoneInID());
			dtTrainPlan.setStationInID(tTrainPlan.getStationInID());
			dtTrainPlan.setFloor(tTrainPlan.getFloor());
			dtTrainPlan.setPeer(tTrainPlan.getPeer());
			dtTrainPlan.setPrice(tTrainPlan.getPrice());
			dtTrainPlan.setNumber(tTrainPlan.getNumber());
			dtTrainPlan.setMemo(tTrainPlan.getMemo());
			dtTrainPlan.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtTrainPlan.setReviser(tSysUser.getUserName());
			trainPlanServiceImpl.save(dtTrainPlan);
			return "redirect:trainplan";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询列车时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/searchtrainplan")
	public String FindTrainPlan(HttpServletRequest request, Model model) {

		String strTrainName = request.getParameter("TrainName");
		if (strTrainName == "") {
			List<VTrainPlan> lstTrainPlan = trainPlanServiceImpl.findAll();
			model.addAttribute("trainplanlist", lstTrainPlan);
			request.setAttribute("strTrainName", strTrainName);
			return "webbackground/train/trainplan";
		} else {
			List<VTrainPlan> lstTrainPlan = trainPlanServiceImpl.findByName(strTrainName);
			model.addAttribute("trainplanlist", lstTrainPlan);
			request.setAttribute("strTrainName", strTrainName);
			return "webbackground/train/trainplan";
		}

	}

}
